package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.UserService;
import service.userserviceimp;

/**
 * Servlet implementation class queryservlet
 */
public class queryservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService userservice=new userserviceimp();
	
	

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String query=request.getParameter("query");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter prw=response.getWriter();
		if(userservice.asks(query)){
			String none="查无此人";
			request.setAttribute("queryname", query);
			request.setAttribute("none", none);
			request.getRequestDispatcher("/query.jsp").forward(request, response);
		}
		else{
			int id=userservice.askuser(query).getId();
			String Sid=String.valueOf(id);
			request.setAttribute("queryname", query);
			request.setAttribute("query", query);
			request.setAttribute("Sid", Sid);
			request.getRequestDispatcher("/query.jsp").forward(request, response);  
		}
	}

}
